验证pytorch是否为GPU版本
x420981611:
(wzh4pytorch) C:\Windows\system32>python
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:29:51) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available() ## 输出应该是True
True
>>>
>>> t=torch.nn.Linear(3,3)
>>> t.to("cuda:0")
Linear(in_features=3, out_features=3, bias=True)
>>>
>>> input=torch.randn((3,3)).requires_grad_().to("cuda:0")
>>> output=t(input)
>>>
>>> loss=torch.sum(output)
>>> torch.autograd.grad(loss,input,retain_graph=True) ## 输出应该是一个gpu上的梯度矩阵
(tensor([[ 0.2812, 0.3565, -0.3112],
[ 0.2812, 0.3565, -0.3112],
[ 0.2812, 0.3565, -0.3112]], device='cuda:0'),)
>>> loss.backward()
>>> ^Z
(wzh4pytorch) C:\Windows\system32>nvidia-smi
Sun Oct 30 07:14:45 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 526.47 Driver Version: 526.47 CUDA Version: 12.0 |
|-------------------------------+-
|